﻿
Public Class BLLRecurso
    Dim objDAO As DALRecurso
    Dim dg As DataGridView
    Private nome As String
    Private patrimonio As String
    Private obs As String
    Private status As String
    Private motivo As String
    Private datacad As String
    Private codigo As Integer

#Region "Propriedades"
    Public Property pobs As String
        Get
            Return Me.obs
        End Get
        Set(ByVal value As String)
            Me.obs = value
        End Set
    End Property
    Public Property pdatacad As String
        Get
            Return Me.datacad
        End Get
        Set(ByVal value As String)
            Me.datacad = value
        End Set
    End Property
    Public Property pmotivo As String
        Get
            Return Me.motivo
        End Get
        Set(ByVal value As String)
            Me.motivo = value
        End Set
    End Property
    Public Property pcodigo As Integer
        Get
            Return Me.codigo
        End Get
        Set(ByVal value As Integer)
            Me.codigo = value
        End Set
    End Property
    Public Property pstatus As String
        Get
            Return Me.status
        End Get
        Set(ByVal value As String)
            Me.status = value
        End Set
    End Property
    Public Property ppatrimonio As String
        Get
            Return Me.patrimonio
        End Get
        Set(ByVal value As String)
            Me.patrimonio = value
        End Set
    End Property
    Public Property pnome As String
        Get
            Return Me.nome
        End Get
        Set(ByVal value As String)
            Me.nome = value
        End Set
    End Property

#End Region
#Region "Métodos"

    Sub New(ByVal codProd As Integer)
        carrega(codProd)
    End Sub
    Sub carrega(ByVal codrec As Integer)
        Dim tab As DataTable
        Try
            objDAO = New DALRecurso
            tab = objDAO.localizar(codrec)
            If (tab.Rows.Count > 0) Then
                Me.nome = tab.Rows(0)("rec_descricao").ToString
                Me.codigo = tab.Rows(0)("rec_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
    End Sub
    Function Inserir() As Boolean
        Dim ret As Boolean
        Try
            objDAO = New DALRecurso
            If ValidaDuplicata(patrimonio, 0) Then
                ret = objDAO.Inserir(Me.nome, Me.patrimonio, Me.obs, Me.status, Me.motivo, Me.datacad)
            Else
                ret = False
            End If
        Catch ex As Exception
            Throw ex
        End Try
        Return ret
    End Function
    Function localizar(ByVal descricao As String) As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALRecurso
            tab = objDAO.localizar(descricao)
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function localizar(ByVal codigo As Integer) As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALRecurso
            tab = objDAO.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.nome = tab.Rows(0)("rec_descricao").ToString
                Me.codigo = tab.Rows(0)("rec_codigo").ToString
                Me.patrimonio = tab.Rows(0)("rec_patrimonio").ToString
                Me.obs = tab.Rows(0)("rec_obs").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function localizarTudo() As DataTable
        Dim tab As DataTable
        Try
            objDAO = New DALRecurso
            tab = objDAO.localizarTudo()
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return tab
    End Function
    Function Excluir(ByVal codigo As Integer) As Boolean
        Dim ret As Boolean
        Dim tab As DataTable
        Try
            objDAO = New DALRecurso
            tab = objDAO.ValidaExclusao(codigo)
            If tab.Rows.Count = 0 Then
                ret = objDAO.Excluir(codigo)
            Else
                ret = objDAO.ExcluirUpdate(codigo)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            tab = Nothing
            objDAO = Nothing
        End Try
        Return ret
    End Function
    Function ValidaDuplicata(ByVal patrimonio As String, ByVal codigo As String) As Boolean
        Dim ret As Boolean
        Dim tab As DataTable
        Try
            objDAO = New DALRecurso
            tab = objDAO.ValidaDuplicata(patrimonio, codigo)
            If tab.Rows.Count = 0 Then
                ret = True
            Else
                ret = False
            End If
        Catch ex As Exception
            Throw ex
        Finally
        End Try
        Return ret
    End Function
    Function Alterar() As Boolean
        Dim ret As Integer
        Try
            objDAO = New DALRecurso
            If (ValidaDuplicata(patrimonio, codigo)) Then
                ret = objDAO.Alterar(Me.nome, Me.patrimonio, Me.obs, Me.status, Me.motivo, Me.datacad, Me.codigo)
            Else
                ret = False
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAO = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "Construtores"
    Sub New()

    End Sub
#End Region
End Class
